1215B - The Number of Products - CodeForces Solution


combinatorics dp implementation *1400

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
    if i > 0:
        b.append(1)
    else:
        b.append(0)
l, r = [], []
c = 1
for i in b:
    if not i:
        l.append(c)
        c = 0
    c += 1
c = 1
for i in reversed(b):
    if not i:
        r.append(c)
        c = 0
    c += 1
r.reverse()
u = 0
m = len(l)
for i in range(2):
    s = 0
    for j in range(i, m, 2):
        s += r[j]
    for j in range(i, m, 2):
        u += l[j] * s
        s -= r[j]
ans = [u, n * (n + 1) // 2 - u]
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>

using std::vector;

using std::string;



namespace IO {

int fasti() {

#ifdef ONLINE_JUDGE

    const int L = 1 << 21;

    static char ibuf[L], *iS, *iT;

    return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++;

#else

    return getchar();

#endif

}



void read(int& res) {

    res = int();

    bool flag = false;

    char ch = fasti();

    for (; !isdigit(ch); ch = fasti())

        flag |= ch == '-';

    for (; isdigit(ch); ch = fasti())

        (res *= 10) += ch - 48;

    if (flag) res = -res;

}



void read(string& s) {

    s = string();

    char ch = fasti();

    while (isspace(ch)) ch = fasti();

    for (; !isspace(ch); ch = fasti())

        s.push_back(ch);

}



template <class Header, class ...Args>

void read(Header& header, Args &...args) {

    read(header);

    read(args...);

}

}



int main() {

    int n;

    IO::read(n);

    vector<int> a(n);

    for (int i = 0; i < n; i++) {

        IO::read(a[i]);

        a[i] = a[i] < 0;

    }

    int64_t ans[] = { 0, 0 };

    int cnt[] = { 1, 0 };

    for (int i = 0, pre = 0; i < n; i++) {

        pre = pre ^ a[i];

        for (int i : { 0, 1 })

            ans[i] += cnt[pre ^ i];

        cnt[pre]++;

    }

    printf("%lld %lld\n", ans[1], ans[0]);

    return 0;

}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations